﻿var hg_time = null;
var lgjz = "";
var ltplj = "";
var yqd = 0;
var ychjl = 0;
var yixianshi = 0;
var diag;
var imgs = new Array();
var cxszximg = null;
var bz = 0;
var canvas;
var width;
var height;
var xLast;
var yLast;
var mouseX;
var mouseY;
var brushDiameter;
var ctx;
var flag;
var lcfh = null;
var xsyczb = -1;

$(document).ready(function(){
    huabanzhunbei();
    kaishihuoqu2();
    document.title = '.';
    
});
function preLoadImg(url) {
  var img = new Image();
  img.src = url;
  return img;
}

function kaishihuoqu2() {
  yqd = 1;
  var huidiao = document.getElementById("huidiao");
  var huidiao2 = document.getElementById("huidiao2");
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "sz/hd.php?&r=" + Math.random().toString(), true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function() {
    var XMLHttpReq = xhr;
    if (XMLHttpReq.readyState == 4) {
      if (XMLHttpReq.status == 200) {
        var text = XMLHttpReq.responseText;
        var strs = text.split("|Anters_fg|");
        if (strs.length >= 1) {
          var gjzzs = strs[0];
          if (lgjz != strs[0]) {
            var strss = gjzzs.split("<br />");
            for(var i = 0,gjzzs = '';i < strss.length;i ++){
                gjzzs += strss[i];
                if(i == 0)gjzzs += '<br />';
                else if(i < strss.length - 1)gjzzs += '_';
            }
            
            huidiao.innerHTML = gjzzs;
            lgjz = strs[0];
            document.getElementById("bt_xd").style.backgroundImage = "url(images/bwl_n_xd.png)";
          }
          if (strs.length >= 2 && strs[1] != ltplj) {
            var lsHTML;
                //lsHTML  = "<div class='imgbox'><a href='"+strs[1]+"'><img src='" + strs[1] + "'></img></a></div>";
                //var lssrc = "sz/" + sjh + "/TP." + strs[2] + "?r="+Math.random().toString();
            lsHTML = 'images?url=' + strs[1];
            huidiao2.innerHTML = '';
            huidiao2.appendChild(preLoadImg(lsHTML));
            //src = lsHTML;
            //;
            huidiao.style.background="green";
            setTimeout(function(){huidiao.style.background="transparent";},250);
            ltplj = strs[1];
            document.getElementById("bt_ty").style.backgroundImage = "url(images/bwl_n_ty.png)";
          }
        }
        if (hg_time) clearTimeout(hg_time);
        hg_time = setTimeout("kaishihuoqu2();", 1500);
      }
    }
  };
  xhr.send();
  hg_time = setTimeout("kaishihuoqu2();", 3500);
}
var goToWhere = function(where) {
  var me = this;
  me.site = [];
  me.sleep = me.sleep ? me.sleep : 16;
  me.fx = me.fx ? me.fx : 6;
  clearInterval(me.interval);
  var dh = document.documentElement.scrollHeight || document.body.scrollHeight;
  var height = !! where ? dh : 0;
  me.interval = setInterval(function() {
    var top = document.documentElement.scrollTop || document.body.scrollTop;
    var speed = (height - top) / me.fx;
    if (speed === me.site[0]) {
      window.scrollTo(0, height);
      clearInterval(me.interval);
    }
    window.scrollBy(0, speed);
    me.site.unshift(speed);
  }, me.sleep);
};

function xianshiT(a) {
  a.style.backgroundImage = "url(images/bwl_ty.png)";
  if (yqd != 1) return;
  if (yixianshi == 0) {
    yixianshi = 1;
    diag = new Dialog();
    diag.Width = 310;
    diag.Height = 400;
    diag.bottom = 0;
    diag.Title = "图片";
    diag.InvokeElementId = "huidiao_d";
    diag.CancelEvent = function() {
      yixianshi = 0;
      diag.close();
    };
    diag.Modal = false;
    diag.show();
  } else {
    yixianshi = 0;
    diag.close();
  }
}

function focuswbsrwz() {
  var wbsrwz = document.getElementById("wbsrwz");
  wbsrwz.value = ylwz;
  wbsrwz.focus();
}

function qkbwl() {
  var wbsrwz = document.getElementById("wbsrwz");
  var yonghutubiao = document.getElementById("yonghutubiao");
  wbsrwz.value = ylwz;
  if (hg_time) clearTimeout(hg_time);
  yonghutubiao.style.backgroundImage = "url(images/ball.png)";
  if (yixianshi == 1) {
    yixianshi = 0;
    diag.close();
  }
  var huidiao = document.getElementById("huidiao");
  huidiao.innerHTML = "";
  yqd = 0;
  hg_time = null;
  lgjz = "";
  ltplj = "";
  goToWhere(0);
}

function msfh() {
  setTimeout("goToWhere(0)", 2000);
}
function daodibu(a) {
  a.style.backgroundImage = "url(images/bwl_xd.png)";
  $('html, body').animate({
    scrollTop: $("#huidiao").offset().top
  }, 300);
}
function qingkong(){
    bz = 0;
    imgs = new Array();
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    $("#top_cx").css("background-image","url(images/tyb_cx_a.png)");
    $("#top_cf").css("background-image","url(images/tyb_cf_a.png)");
}
function xsyc(a){
    if(ychjl == 0 && (a == 1 || a == 0)){
        scrollTo(0,0);
        if(yqd != 0){
            ychjl = 1;
            $("#gjl").hide();
            $("#xx").show();
        }
    }else{
        ychjl = 0;
        $("#gjl").show();
        $("#xx").hide();
    }
}
function zbxsyc(a){
    if(xsyczb == 0)xsyczb = -1;
    else xsyczb = 0;
}


function onMouseMove(evt) {
  evt.preventDefault();
  if (flag) {
    var p = pos(evt);
    ctx.lineTo(p.x, p.y);
    ctx.lineWidth = 2.0; // 设置线宽
    //ctx.shadowColor = "#fc1741";
    ctx.shadowBlur = 1;
    //ctx.shadowOffsetX = 6;
    ctx.stroke();
  }
}

function onMouseDown(evt) {
  evt.preventDefault();

    if(bz < imgs.length)imgs = imgs.slice(0,bz);
    imgs.push(ctx.getImageData(0,  0,  canvas.width,  canvas.height));
    bz ++;
    qhcxcftb();
  ctx.beginPath();
  var p = pos(evt);
  ctx.moveTo(p.x, p.y);
  flag = true;
}

function onMouseUp(evt) {
  evt.preventDefault();
  flag = false;
  xsyc(-1);
}
function pos(event) {
  var x, y;
  if (isTouch(event)) {
    x = event.touches[0].pageX;
    y = event.touches[0].pageY;
  } else {
    x = event.layerX;
    y = event.layerY;
  }
  return {
    x: x,
    y: y
  };
}

function isTouch(event) {
  var type = event.type;
  if (type.indexOf('touch') >= 0) {
    return true;
  } else {
    return false;
  }
}

function getWidth() {
  var xWidth = null;
  if (window.innerWidth !== null) {
    xWidth = window.innerWidth;
  } else {
    xWidth = document.body.clientWidth;
  }
  return xWidth;
}
function getHeight() {
  var xHeight = null;
  if (window.innerHeight !== null) {
    xHeight = window.innerHeight;
  } else {
    xHeight = document.body.clientHeight;
  }
  return xHeight;
}
function ty_cx(){
    if(bz > 0){
        if(bz >= imgs.length)cxszximg = ctx.getImageData(0,  0,  canvas.width,  canvas.height);
        ctx.putImageData(imgs[--bz],  0,  0);
        qhcxcftb();
    }
}
function ty_cf(){
    if(bz < imgs.length){
        var ximg;
        if(bz >= imgs.length - 1)ximg = cxszximg;
        else 
            ximg = imgs[bz + 1];
        ctx.putImageData(ximg,  0,  0);
        bz++;
        qhcxcftb();
    }
}
function qhcxcftb(){
    if(bz > 0)$("#top_cx").css("background-image","url(images/tyb_cx_l.png)");
    else $("#top_cx").css("background-image","url(images/tyb_cx_a.png)");
    if(bz >= imgs.length)$("#top_cf").css("background-image","url(images/tyb_cf_a.png)");
    else $("#top_cf").css("background-image","url(images/tyb_cf_l.png)");
}
function draw(x,y){
		ctx.strokeStyle = '#000000';
		ctx.beginPath();
  		ctx.moveTo(xLast, yLast);		
  		ctx.lineTo(x, y);
  		ctx.stroke();
          
  		// Chalk Effect
		var length = Math.round(Math.sqrt(Math.pow(x-xLast,2)+Math.pow(y-yLast,2))/(5/brushDiameter));
		var xUnit = (x-xLast)/length;
		var yUnit = (y-yLast)/length;
		xLast = x;
		yLast = y;
}
function huabanzhunbei(){
    canvas = document.getElementById('myCanvas');
    canvas.addEventListener('mousemove', onMouseMove, false);
    canvas.addEventListener('mousedown', onMouseDown, false);
    canvas.addEventListener('mouseup', onMouseUp, false);
    canvas.addEventListener('touchstart', function(evt) {
            //evt.preventDefault();
            var touch = evt.touches[0];
            mouseX = touch.pageX;
            mouseY = touch.pageY;
            xLast = mouseX;
            yLast = mouseY;
            if(bz < imgs.length)imgs = imgs.slice(0,bz);
            imgs.push(ctx.getImageData(0,  0,  canvas.width,  canvas.height));
            bz ++;
            draw(mouseX + 1, mouseY + 1);
            qhcxcftb();
            if(xsyczb == 0)xsyc(1);
        }, false);
    document.addEventListener('touchend', function(evt) {
            mouseD = false;
        }, false);
    canvas.addEventListener('touchmove', function(evt) {
            var touch = evt.touches[0];
            mouseX = touch.pageX;
            mouseY = touch.pageY;
            if (mouseY < height && mouseX < width) {
                evt.preventDefault();
                //$('.chalk').css('left', mouseX + 'px');
                //$('.chalk').css('top', mouseY + 'px');
                //$('.chalk').css('display', 'none');
            draw(mouseX, mouseY);
            }
        }, false);
    canvas.addEventListener('touchend', onMouseUp, false)
    $('#myCanvas').css('width',$(window).width());
    $('#myCanvas').css('height',$(window).height());
    canvas.width = $(window).width();
    canvas.height = $(window).height();
    width = canvas.width;
    height = canvas.height;
    xLast = 0;
    yLast = 0;
    mouseX;
    mouseY;
    brushDiameter = 7;
    ctx = canvas.getContext('2d');
    ctx.lineWidth = 2.0; // 设置线宽
    ctx.strokeStyle = "#000000"; // 设置线的颜色
    flag = false;
}
function liangcifanhui(){
    if(lcfh != null)window.history.back();
    else{
        lcfh = setTimeout(function(){
            clearTimeout(lcfh);
            lcfh = null;
        },700);
    }   
}